package com.yyb.learn.jbasics.basic.JAVA8.lambda;


import com.yyb.learn.jbasics.basic.JAVA8.bean.Apple;
import com.yyb.learn.jbasics.basic.JAVA8.predicate.FilterPredicate;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import org.springframework.core.annotation.Order;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * Lambda常用写法
 */
public class Java8 {

    public static void main(String[] args) {
        List<Apple> appleList = Arrays.asList(new Apple("green", 2D),
                new Apple("green", 1.5D),
                new Apple("red", 1D));
//        List<Apple> redApples = filter(appleList, (Apple apple) -> "red".equals(apple.getColor()));
//        List<Apple> weightApples2 = filter(appleList, (Apple apple) -> (1.5 < apple.getWeight()));
//        List<Integer> intList = Arrays.asList(new Integer(1),new Integer(2),new Integer(3));
//        List<Integer> lessThan3 = filter(intList, (i) -> i < 3);
//        System.out.println(lessThan3.size() + "|" + lessThan3.get(0) + "-" + lessThan3.get(1));

        appleList.sort((Apple ap1, Apple ap2) -> ap1.getWeight().compareTo(ap2.getWeight()));
        System.out.println(appleList.get(0) + "|" + appleList.get(1) + "|" + appleList.get(2));

        Thread thread = new Thread(() -> System.out.println("1"));


    }

    /**
     * 行为参数化
     *
     * @param tarList
     * @param predicate
     * @return
     */
    private static <T> List<T> filter(List<T> tarList, FilterPredicate<T> predicate) {
        List<T> result = new ArrayList<>();
        for (T t : tarList) {
            if (predicate.filter(t)) {
                result.add(t);
            }
        }
        return result;
    }


}
